home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / palis102.lha / Palis / PatchTest / src / Chk.c next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  278 lines

  1. /*
  2.     ·C·O·D·E·X· ·D·E·S·I·G·N· ·S·O·F·T·W·A·R·E·
  3.     presents
  4.  
  5.     TestPatch
  6.  
  7.     this program will check whether library patching is observed by
  8.     a PatchManager like SaferPatches or PALIS.
  9.     returns appropiate codes to the caller.
  10.  
  11.     FILE:    Chk.c
  12.     TASK:    all stuff... ;-)
  13.  
  14.     (c)1996 by Hans Bühler, codex@stern.mathematik.hu-berlin.de
  15. */
  16.  
  17. #include    "chk.h"
  18. #include    <stdio.h>
  19.  
  20. // ---------------------------
  21. // defines
  22. // ---------------------------
  23.  
  24. #define    ARG_LIB            0
  25. #define    ARG_OFF            1
  26. #define    ARG_QUIET        2
  27. #define    ARG_NUM            3
  28.  
  29. #define    BUFLEN            512
  30.  
  31. #define    GET_LIB            ((char *)Array[ARG_LIB])
  32. #define    GET_OFF            (*((LONG *)Array[ARG_OFF]))
  33. #define    GET_QUIET        ((BOOL)Array[ARG_QUIET])
  34.  
  35. #define    errprintf            if(!GET_QUIET)    \
  36.                                         printf(
  37.  
  38. #define    JMP_CODE                0x4ef9
  39.  
  40. // ---------------------------
  41. // datatypes
  42. // ---------------------------
  43.  
  44. struct Func
  45.     {
  46.         UWORD    Jmp;
  47.         APTR    OldFunc;
  48.     };
  49.  
  50. // ---------------------------
  51. // proto
  52. // ---------------------------
  53.  
  54. extern int main(int argc, char **argp);
  55.  
  56. // ---------------------------
  57. // vars
  58. // ---------------------------
  59.  
  60. struct RDArgs
  61.     *OwnArgs            =    0,
  62.     *Args                =    0;
  63.  
  64. static char
  65.     *Template        =    "LIB=LIBRARY/A,OFF=OFFSET/N/A,QUIET/S";
  66.  
  67. ULONG
  68.     Array[ARG_NUM]    =    {    0,
  69.                                 0,
  70.                                 FALSE
  71.                             };
  72.  
  73. static char
  74.     *InfoTxt    =    PROGNAME_FULL " by Hans Bühler, Codex Design Software:\n"
  75.                     "\n"
  76.                     "This program tests whether patching libraries is observed\n"
  77.                     "by any patch manager as SaferPatches etc.\n"
  78.                     "\n\x9b" "2;32;41m"
  79.                     "+------------------------------------------------------+\n"
  80.                     "| WARNING : Misuse of this program will for sure crash |\n"
  81.                     "| your computer !                                      |\n"
  82.                     "| Note that the author cannot be held liable for _any_ |\n"
  83.                     "| damage caused by the use or misuse of this program ! |\n"
  84.                     "+------------------------------------------------------+\n"
  85.                     "\x9b" "0m\n"
  86.                     "LIBRARY¹ : Name of library to patch for a test.\n"
  87.                     "OFFSET/N¹: Function offset to patch for a test.\n"
  88.                     "QUIET    : No output.\n"
  89.                     "\n"
  90.                     " ¹: Please note " LIBNAME " which might be used without\n"
  91.                     "    causing problems.\n"
  92.                     "\n"
  93.                     "This program comes along with the dev/misc/palis archive.\n"
  94.                     "It and its source is freeware ;^)\n"
  95.                     "\n"
  96.                     " [<Hans Bühler>:codex@stern.mathematik.hu-berlin.de]\n",
  97.     *WarnTxt    =    "\n"
  98.                     "\x9b" "2;32;41m"
  99.                     "+----------------------------------------------------+\n"
  100.                     "| WARNING: This program is for _expert_ users only ! |\n"
  101.                     "|          It will perform  risky operations in your |\n"
  102.                     "|          system thus PLEASE read the manual before |\n"
  103.                     "|          continuing !                              |\n"
  104.                     "| Type '?' twice to receive short information.       |\n"
  105.                     "+----------------------------------------------------+\n"
  106.                     "\x9b" "0m\n"
  107.                     "Are you sure you want to continue [n]: ",
  108.     *ErrHeader=    PROGNAME " error: ";
  109.  
  110. static struct Func
  111.     Func1    =    {
  112.                     JMP_CODE,
  113.                     0
  114.                 },
  115.     Func2    =    {
  116.                     JMP_CODE,
  117.                     0
  118.                 };
  119.  
  120. // ---------------------------
  121. // funx: job...
  122. // ---------------------------
  123.  
  124. static int ChkPatch(void)
  125. {
  126.     APTR                dummy;
  127.     struct Library    *lib;
  128.     int                ret;
  129.  
  130.     errprintf "Opening library '%s'... ",GET_LIB);
  131.  
  132.     if(!( lib = OpenLibrary(GET_LIB,0) ))
  133.     {
  134.         errprintf "FAILED !\n");
  135.         return RETURN_FAIL;
  136.     }
  137.  
  138.     errprintf "okay.\n");
  139.  
  140.     if(!GET_QUIET)
  141.     {
  142.         char    c;
  143.  
  144.         printf(PROGNAME " will now install 2 patches to '%s/%ld'\n"
  145.                  " and will then try to remove them in reverse order.\n"
  146.                  " Is that okay for you [n]: ",GET_LIB,GET_OFF);
  147.  
  148.         scanf("%c",&c);fflush(stdin);
  149.  
  150.         if((c | ' ') != 'y')
  151.         {
  152.             CloseLibrary(lib);
  153.             return RETURN_FAIL;
  154.         }
  155.     }
  156.  
  157.     errprintf "Okay. Operation starts now... ");
  158.  
  159.     Forbid();
  160.  
  161.     Func1.OldFunc    =    SetFunction(lib,GET_OFF,(APTR)&Func1.Jmp);
  162.     Func2.OldFunc    =    SetFunction(lib,GET_OFF,(APTR)&Func2.Jmp);        // == &Func1
  163.  
  164.     dummy                =    SetFunction(lib,GET_OFF,Func1.OldFunc);
  165.  
  166.     if(dummy != &Func1.Jmp)            // no mamanger ! ;-(
  167.     {
  168.         // we don't have to care about the original function since
  169.         // we replaced right this into the library.
  170.         // therefore we are out and everything is fine ;^>
  171.  
  172.         ret    =    RETURN_WARN;
  173.     }
  174.     else
  175.     {
  176.         // well, it seems there's a manager. Therefore we will
  177.         // remove our second function and everything will be cleared
  178.         // up.
  179.  
  180.         SetFunction(lib,GET_OFF,Func2.OldFunc);
  181.  
  182.         ret    =    RETURN_OK;
  183.     }
  184.  
  185.     Permit();
  186.  
  187.     if(!GET_QUIET)
  188.     {
  189.         printf("done.\n\nStatus: ");
  190.         if(ret == RETURN_OK)
  191.             puts("\x9b" "2mAll right !\x9b" "0m\n"
  192.                   "------- It seems there's any manager taking care of you ;-)\n");
  193.         else
  194.             puts("\x9b" "2mNO PATCH MANAGER FOUND !\x9b" "0m\n"
  195.                   "------- Please get one from the net !\n"
  196.                   "        (e.g. util/misc/SaferPatches)\n");
  197.     }
  198.  
  199.     CloseLibrary(lib);
  200.  
  201.     errprintf "Library closed.\n");
  202.  
  203.     return ret;
  204. }
  205.  
  206. // ---------------------------
  207. // funx: warning
  208. // ---------------------------
  209.  
  210. static int Warning(void)
  211. {
  212.     char    c;
  213.  
  214.     if(!GET_QUIET)
  215.     {
  216.         printf(WarnTxt);
  217.         scanf("%c",&c);fflush(stdin);
  218.  
  219.         if ((c | ' ') != 'y')
  220.             return RETURN_FAIL;
  221.  
  222.         putchar('\n');
  223.     }
  224.  
  225.     return RETURN_OK;
  226. }
  227.  
  228. // -----------------------------
  229. // funx: arguments
  230. // -----------------------------
  231.  
  232. static int ParseArgs(void)
  233. {
  234.     if(OwnArgs = AllocDosObject(DOS_RDARGS,0))
  235.     {
  236.         OwnArgs->RDA_ExtHelp    =    InfoTxt;
  237.     }
  238.  
  239.     if(!(Args = ReadArgs(Template,Array,OwnArgs)))
  240.     {
  241.         char    buf[BUFLEN];
  242.  
  243.         Fault(IoErr(),0,buf,BUFLEN);
  244.         errprintf "%s%s.\n",ErrHeader,buf);
  245.         return RETURN_FAIL;
  246.     }
  247.  
  248.     return RETURN_OK;
  249. }
  250.  
  251. static void UnParseArgs(void)
  252. {
  253.     if(Args)
  254.         FreeArgs(Args);
  255.     if(OwnArgs)
  256.         FreeDosObject(DOS_RDARGS,OwnArgs);
  257. }
  258.  
  259. // ---------------------------
  260. // funx
  261. // ---------------------------
  262.  
  263. int main(int argc, char **argp)
  264. {
  265.     int    ret;
  266.  
  267.     if((ret = ParseArgs()) == RETURN_OK)
  268.     {
  269.         if((ret = Warning()) == RETURN_OK)
  270.         {
  271.             ret    =    ChkPatch();
  272.         }
  273.     }
  274.     UnParseArgs();
  275.  
  276.     exit(ret);
  277. }
  278.